<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <script src="../vue.js"></script>
  </head>

  <body>
    <div id="app">
      <input type="text" v-model="search" />
      <ul>
        <li v-for="item in filterPersons" :key="item.id">
          name---{{item.name}}---sex---{{item.sex}}---age---{{item.age}}
        </li>
      </ul>
      <button @click="order=0">年龄升序</button>
      <button @click="order=1">年龄降序</button>
    </div>
    <script>
      Vue.config.productionTip = false;

      const vm = new Vue({
        el: "#app",
        data() {
          return {
            persons: [
              { id: 1, name: "周冬雨", sex: "女", age: 20 },
              { id: 2, name: "马冬梅", sex: "女", age: 30 },
              { id: 3, name: "周杰伦", sex: "男", age: 15 },
              { id: 4, name: "林俊杰", sex: "男", age: 70 },
            ],
            search: "",
            //定义一个数据 0代表升序 1代表降序 默认升序
            order: 0,
          };
        },
        computed: {
          filterPersons() {
            //先过滤
            const filterPersons = this.persons.filter((item) =>
              item.name.includes(this.search)
            );
            //再排序
            return filterPersons.sort((a, b) => {
              return this.order === 0 ? a.age - b.age : b.age - a.age;
            });
          },
        },
      });
    </script>
  </body>
</html>
